<?php

namespace catchAdmin\system\controller;

use app\common\model\BaseModel;
use app\common\model\CollectClass;
use catchAdmin\system\request\PacketConfigRequest;
use catchAdmin\system\request\sensitiveWord\CreateRequest;
use catchAdmin\system\request\sensitiveWord\UpdateRequest;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\exceptions\ValidateFailedException;
use MongoDB\BSON\ObjectId;
use think\exception\ValidateException;
use think\Request;

class PacketConfig extends CatchController
{
    protected \app\common\model\PacketConfig $model;

    public function __construct()
    {
        $this->model = new \app\common\model\PacketConfig();
    }

    /**
     * 列表
     *
     * @time 2020年06月17日
     * @return \think\response\Json
     */
    public function index()
    {
        return CatchResponse::paginate($this->model->getLists());
    }

    /**
     * 保存
     *
     * @time 2020年06月17日
     * @param CreateRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request)
    {
        $postData = $request->param();
        try {
            validate(PacketConfigRequest::class)->check($postData);
            //根据部门id判断是否已经设置了
            $count = $this->model->where([["dept_id", "=", (int)$postData["dept_id"]]])->count();
            if ($count > 0) {
                //throw new ValidateFailedException("当前科室已经绑定过配置，请不要重新绑定");
            }
            foreach ($postData["weeks"] as $k => &$v) {
                $v["id"] = (string)new ObjectId();
            }
            foreach ($postData["diseases"] as $k => &$v) {
                $v["id"] = (string)new ObjectId();
            }

            $result = $this->model->createBy($postData);
            if ($result) {
                $postData["packet_id"] = $result;
                //执行分组表的写入操作
                $rst = (new CollectClass())->bindClass($postData);
                if ($rst !== null) {
                    return CatchResponse::success([], "创建成功");
                }
            }
            return CatchResponse::fail("创建失败，请联系管理员");

        } catch (ValidateException $exception) {
            throw new ValidateFailedException($exception->getMessage());
        }
    }

    /**
     * 更新
     *
     * @time 2020年06月17日
     * @param $id
     * @param UpdateRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request)
    {
        $postData = $request->param();


        foreach ($postData["weeks"] as $k => &$v) {
            if (!isset($v["id"])) {
                $v["id"] = (string)new ObjectId();
            }
        }
        foreach ($postData["diseases"] as $k => &$v) {
            if (!isset($v["id"])) {
                $v["id"] = (string)new ObjectId();
            }
        }



        $result = $this->model->updateBy($id, $postData);
        if ($result) {
            //执行分组表的写入操作
            $rst = (new CollectClass())->updateClass($postData, $id);
            if ($rst !== false) {
                return CatchResponse::success([], "更新成功");
            }
        }
        return CatchResponse::success();
    }

    /**
     * 删除
     *
     * @time 2020年06月17日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        $result = $this->model->deleteBy($id);
        if ($result) {
            //根据id删除对应下面的分组
            return CatchResponse::success((new CollectClass())->where([["packet_id", "=", $id]])->save(["deleted_at" => time()]));
        }
        return CatchResponse::fail("删除失败");
    }

    public function read($id): \think\response\Json
    {
        return CatchResponse::success($this->model->find($id));
    }

}
